#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>
#include <openssl/ssl.h>
#include <openssl/opensslv.h>





int main()
{
    //1.实例化(底层网络通信框架)的I/O事件监控句柄--即如果有I/O通信，将会通过信号告知
    auto* loop = EV_DEFAULT;
    //2.实例化libEvHandler句柄，将AMPQ框架与事件关联起来--即将监控到的事件交给libev处理
    AMQP::LibEvHandler handler(loop);
    //3.实例化连接对象，可以使用连接对象将RabbitMQ服务器与事件处理关联起来--即使用RabbitMQ服务器进行AMQP的序列与反序列
    AMQP::Address address("amqp://root:123456@127.0.0.1:5672/");
    AMQP::TcpConnection connection(&handler, address);
    //4.实例化信道对象，channel是虚拟连接，同一个channel可以连接多个客户端
    AMQP::TcpChannel channel(&connection);
    //5.声明交换机
     channel.declareExchange("test_exchange",AMQP::ExchangeType::direct)
    .onError([](const char* message)
    {
        std::cout<<"声明交换机失败:"<<message<<std::endl;
        exit(0);
    })
    .onSuccess([]()
    {
        std::cout<<"声明交换机成功!"<<std::endl;
    });
    //6.声明交换队列
    channel.declareQueue("test_queue")
    .onError([](const char* message)
    {
        std::cout<<"声明队列失败:"<<message<<std::endl;
        exit(0);
    })
    .onSuccess([]()
    {
        std::cout<<"声明队列成功"<<std::endl;
    });
    //7.管道的交换机和队列进行绑定,
    channel.bindQueue("test_exchange","test_queue","test_queue_key")
    .onError([](const char* message)
    {
        std::cout<<"管道的交换机和队列绑定失败:"<<message<<std::endl;
        exit(0);
    })
    .onSuccess([]()
    {
        std::cout<<"管道的交换机和队列绑定成功"<<std::endl;
    });
    //8.订阅队列消息 -- 设置消息处理回调函数
    auto MessageCB = [](AMQP::TcpChannel* channel,const AMQP::Message& message,uint64_t deliveryTag, bool redelivered)
    {
        std::cout<<std::string(message.body(),message.bodySize())<<std::endl;
        channel->ack(deliveryTag);
    };
    auto callback = std::bind(MessageCB,&channel,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3);
    channel.consume("test_queue","test_consume_tag")
    .onReceived(callback)
    .onError([](const char* message)
    {
        std::cout<<"订阅队列消息失败:"<<message<<std::endl;
        exit(0);
    });
    ev_run(loop,0);
    return 0;


}